import { Component } from 'react'
import { View, Image } from '@tarojs/components'
import config from '../../config.json'
import Taro from '@tarojs/taro';
import JSEncrypt from 'jsencrypt';
import {
    AtButton,
    AtForm,
    AtInput,
    AtMessage
} from "taro-ui";
import "./login.less"

export default class Index extends Component<any, any> {

    constructor(props) {
        super(props);
        this.state = {
            id: "",
            password: "",
            loading: false
        }
    }

    componentWillMount() {

    }

    componentDidMount() { }

    componentWillUnmount() { }

    componentDidShow() { }

    componentDidHide() { }

    onChange(key: string, value: string) {
        let obj: any = {};
        obj[key] = value;
        this.setState(obj)
    }

    async onLogin() {
        let encryptor = new JSEncrypt();
        let pubkey = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChfxqaOOY9c6ihOEiujQyWNMPrnnj6syEvaDSQ+shAQY0QXotvtGOZ5lE6GBqvjJqo/0csbtWmZwQX5+vrZnA3Hoc1t22v6SUkfryog/kM3xge9Egk+RVBcrvkQdp88K2I/q31iRmQblVkXqyaXTZZ7iP+/jsv6GYOaim376kSuwIDAQAB-----END PUBLIC KEY-----"
        let _this = this;
        encryptor.setPublicKey(pubkey);
        let rsapassword = encryptor.encrypt( _this.state.password);
        console.log(this.state)
        if (this.state.id && this.state.password) {
            this.setState({ loading: true });
            let res = await Taro.request({
                url: config.domainName + "/user/login",
                method: "POST",
                data: {
                    id: _this.state.id,
                    password: rsapassword
                },
                dataType: "json"
            });
            this.setState({ loading: false });
            if (!res.data.code) {
                Taro.atMessage({
                    message: "登录成功",
                    type: "success"
                });
                Taro.setStorageSync("token", res.header.token)
                setTimeout(() => {
                    if (res.data.data.first) {
                        Taro.navigateTo({
                            url: "/pages/userinfo/userinfo?flag=2"
                        });
                    } else {
                        Taro.switchTab({
                            url: "/pages/index/index"
                        });
                    }
                }, 1000);
            } else {
                Taro.atMessage({
                    message: res.data.msg,
                    type: "error"
                })
            }
        }
    }

    render() {
        return (
            <View style={{ display: "flex", alignItems: "center", height: "100vh", justifyContent: "center" }}>
                <View style={{ width: "90%", marginTop: "-3rem" }}>
                    <AtMessage />
                    <View style={{
                        width: "6rem",
                        height: "6rem",
                        margin: "auto"
                    }}>
                        <Image style={{ width: "100%", height: "100%" }} src={config.domainName + "/xiyou.png"}></Image>
                    </View>
                    <View style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginRight: 20 }}>
                        <View className="module-title">登录</View>
                    </View>
                    <AtInput value={this.state.id} name="id" placeholder="账号/一卡通号码" onChange={this.onChange.bind(this, "id")}></AtInput>
                    <AtInput value={this.state.password} type="password" name="pwd" placeholder="密码/教务系统密码" onChange={this.onChange.bind(this, "password")}></AtInput>
                    <View style={{ height: "20px" }}>

                    </View>
                    <AtButton type="primary" onClick={this.onLogin.bind(this)} loading={this.state.loading}>登录</AtButton>
                    <View style={{
                        fontSize: "0.6rem",
                        marginTop: "0.5rem",
                        color: "#014A95",
                        marginLeft: "1rem"
                    }}>首次登录教职工账号为一卡通号码，密码为教务系统密码</View>

                </View>
            </View>
        )
    }
}

